package com.xpec.c4.game.login;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.account.Account;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.datamanager.agent.AccountData;
import com.xpec.c4.game.datamanager.agent.AccountDataAgent;
import com.xpec.c4.game.login.async.AccountLoginRequestGetAccountData;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.h5server.H5ServerOperations;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.ServerProperties;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.server.CloseType;
import com.xpec.c4.service.server.ServerOperations;


/**
 * LoginModule自行override的版本
 * @author brian_yang
 *
 */
public class AccountDataAgentLogin extends AccountDataAgent{

	private static final Log log = LogFactory.getLog(AccountDataAgentLogin.class);
	
	/**
	 * 
	 * @param module
	 */
	public AccountDataAgentLogin(BasicServiceModule module) {
		super(module);
	}
	
	@Override
	public void queryDataCallback(BaseDataInfo obj, Object... userObject){
		
		super.queryDataCallback(obj, userObject);
		
		switch(obj.getResultCode()){
		
		case ResultNo.OK:
		{
			AccountData q = (AccountData)obj;
			
			if(userObject!=null && userObject[0] != null){
				if(q.getData() != null){
					AccountLoginRequestGetAccountData data = (AccountLoginRequestGetAccountData)userObject[0];
					data.account = (Account) q.getData(); 
					((LoginModule)this.getModule()).accountLoginRequestGetAccountResultCallback(data);
				}
			}		
			//break;
			return;
		}
		
		// 上一層有印相關Log
		case ResultNo.ACCESS_DENY:
			System.out.println("Access deny  id: " + obj.getKey());
			break;
			
		case ResultNo.FAILED:
			System.out.println("Query failed  id: " + obj.getKey());
			break;
			
		case ResultNo.DATA_NOT_FOUND:
			System.out.println("Data not found id: " + obj.getKey());
			break;
			
		case ResultNo.DB_OPERATION_FAILED :
			System.out.println("DB operation failed id: " + obj.getKey());
			break;
			
		}
		//TODO 既然取不到了要不要就斷線了, 讓client還在那邊一直等
		if(userObject!=null && userObject[0] != null){
			AccountLoginRequestGetAccountData data = (AccountLoginRequestGetAccountData)userObject[0];
			if (ServerProperties.isTargetModule(PlayerCommonInfoManager.getInstance().getGatewayBySessionId(data.sessionID), ModuleName.H5GATEWAY)) {
				H5ServerOperations.sessionClose(data.sessionID, CloseType.KICK_BY_ERROR, this.getModule());
			} else {
				ServerOperations.sessionClose(data.sessionID, CloseType.KICK_BY_ERROR, this.getModule());
			}
		}
	}
	
	/**
	 * 
	 * @param str
	 * @param player
	 * @return
	 */
	public boolean writebackDataToDC(GUID str, Object accountObj, Object... userData) {
		
		AccountData data = new AccountData();
		data.setKey(str);
		
		data.setMessageType(MessageTypes.UPDATE_ACCOUNT_INFO);
		data.setData(accountObj);

		// 產生callback需要的method
		Method mth = getCallbackMethod(this, "writebackDataCallback");		
		return super.dataRequest(this, null, null, this, mth, data, userData);
	}

	public void writebackDataCallback(BaseDataInfo obj, Object... userObject){

		//如果是釋放權限的最後write
		boolean result = obj.getResultCode() == ResultNo.OK ?  true : false;
		if(!result){
			log.error("account write and release owner error: " + obj.getKey()
					+ " error code :" + obj.getResultCode());
		}
		
	}
	
}
